package com.baizhi.filter;

import com.baizhi.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by 小荷~ on 2019/6/16.
 */
@WebFilter(filterName = "myFilter",urlPatterns = "/main/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request1 = (HttpServletRequest) request;

        HttpServletResponse response1 = (HttpServletResponse) response;

        HttpSession session = request1.getSession();

        Admin admin = (Admin) session.getAttribute("admin");

        if(admin != null){

            System.out.println("放行 ");

            chain.doFilter(request,response);

        }else {

            System.out.println("拦截");

            response1.sendRedirect("/houqi/login.jsp");

        }


    }
}
